﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using entities;
using GamesFormsApplication;

namespace GamesFormsApplication
{
    public partial class EditForm : Form
    {
        private const string FirearmsType = "Firearms";
        private const string MeleeType = "Melee";
        private const string MissileType = "Missile";

        private AbstractWeapon weapon;

        private Dictionary<string, Type> types = new Dictionary<string, Type>();

        public EditForm()
        {
            InitializeComponent();

            types.Add(FirearmsType, typeof(Firearms));
            types.Add(MeleeType, typeof(Melee));
            types.Add(MissileType, typeof(Missile));

            foreach (string name in types.Keys)
            {
                typeComboBox.Items.Add(name);
            }

        }
        public AbstractWeapon GetGun()
        {
            return weapon;
        }

        private void okButton_Click(object sender, EventArgs e)
        {

            if ((nameTextBox.Text == "") ||
               (levelTextBox.Text == "") ||
               (damageTextBox.Text == ""))
            {
                MessageBox.Show("Please fill in all fields");
            }
            else
            {   
                
                
                string typeValue = this.typeComboBox.Text;
                Type type = types[typeValue];

                weapon = (AbstractWeapon)type.GetConstructor(new Type[] { }).Invoke(new object[] { });


                int damage = Int32.Parse(this.damageTextBox.Text);
                weapon.Damage = damage;


                string name = this.nameTextBox.Text;
                weapon.Name = name;

                int level = Int32.Parse(this.levelTextBox.Text);
                weapon.Level = level;
                
                if (typeof(Firearms).IsAssignableFrom(type))
                {
                    Firearms firearms = (Firearms)weapon;
                    int capacity = Int32.Parse(this.capacityTextBox.Text);
                    firearms.Capacity = capacity;
                }else

                if (typeof(Melee).IsAssignableFrom(type))
                {
                    Melee melee = (Melee)weapon;
                    int impactSpeed = Int32.Parse(this.impactSpeedTextBox.Text);                    
                    melee.ImpactSpeed = impactSpeed;
                    int distanceDefeat = Int32.Parse(this.distanceDefeatTextBox.Text);
                    melee.DistanceDefeat = distanceDefeat;
                }else
                if (typeof(Missile).IsAssignableFrom(type))
                {
                    Missile missile = (Missile)weapon;
                    int damageRadius = Int32.Parse(this.damegRadiusTextBox.Text);
                    int delay = Int32.Parse(this.delayTextBox.Text);
                    missile.DamageRadius = damageRadius;
                    missile.Delay = delay;
                }   
                this.Close();
            }

        }

        //public AbstractWeapon GetGun()
        //{
        //    return weapon;
        //}

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        

        private void EditForm_Load(object sender, EventArgs e)
        {

        }

        
        private void typeComboBox_TextChanged(object sender, EventArgs e)
        {

        }


        private void typeComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            if (typeComboBox.Text == MeleeType)
            {
                distanceDefeatTextBox.Enabled = true;
                damegRadiusTextBox.Enabled = false;
                impactSpeedTextBox.Enabled = true;
                capacityTextBox.Enabled = false;
                delayTextBox.Enabled = false;
                
            }

            if (typeComboBox.Text == MissileType)
            {
                distanceDefeatTextBox.Enabled = false;
                damegRadiusTextBox.Enabled = true;
                impactSpeedTextBox.Enabled = false;
                capacityTextBox.Enabled = false;
                delayTextBox.Enabled = true;

            }
            if (typeComboBox.Text == FirearmsType)
            {
                distanceDefeatTextBox.Enabled = false;
                damegRadiusTextBox.Enabled = false;
                impactSpeedTextBox.Enabled = false;
                capacityTextBox.Enabled = true;
                delayTextBox.Enabled = false;
                                
            }
        

        }

        private void cancelButton_Click_1(object sender, EventArgs e)
        {
        this.Close();
        }
       
       
    }
}
